package cn.com.nes.site.service.crm.order.impl;

import cn.com.nes.common.util.CopyUtils;
import cn.com.nes.mybatis.agent.ope.entity.SalePriceDetail;
import cn.com.nes.mybatis.agent.ope.mapper.SalePriceDetailMapper;
import cn.com.nes.site.entity.bo.crm.sale.CopySalePriceDetailBO;
import cn.com.nes.site.entity.em.DeleteEnum;
import cn.com.nes.site.service.crm.order.SalePriceDetailService;
import cn.com.nes.site.service.crm.order.bo.QuerySalePriceDetailBO;
import cn.com.nes.site.service.crm.order.bo.SaleDetailBO;
import cn.com.nes.site.service.crm.order.dto.SalePriceDetailDTO;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class SalePriceDetatilServiceImpl extends ServiceImpl<SalePriceDetailMapper, SalePriceDetail> implements SalePriceDetailService {

    /**
     * 复制订单详情
     * @param copySalePriceDetailBO
     */
    @Override
    public void copySalePriceDetail(CopySalePriceDetailBO copySalePriceDetailBO) throws Exception {
        List<SalePriceDetail> salePriceDetails = this.baseMapper.getSalePriceDetatils(copySalePriceDetailBO.getSourcePriceMasterId());
        List<SalePriceDetail> targetSalePriceDetails = new ArrayList<>();
        for (SalePriceDetail sourceSalePriceDetail : salePriceDetails) {
            SalePriceDetail targetSalePriceDetail = new SalePriceDetail();
            BeanUtils.copyProperties(sourceSalePriceDetail, targetSalePriceDetail, "id");
            initSalePriceDetatil(targetSalePriceDetail, copySalePriceDetailBO.getOperateUserId(), copySalePriceDetailBO.getOperateDate(), copySalePriceDetailBO.getOperateUserCompanyId());
            targetSalePriceDetail.setPriceMasterId(copySalePriceDetailBO.getTargetPriceMasterId());
            targetSalePriceDetail.setOrderId(copySalePriceDetailBO.getTargetSaleOrderId());
            log.info("copySalePriceDetail - SalePriceDetail: {}", targetSalePriceDetail);
            targetSalePriceDetails.add(targetSalePriceDetail);
        }
        this.saveBatch(targetSalePriceDetails);
    }

    @Override
    public List<SalePriceDetailDTO> getPriceDetails(Long priceMasterId) {
        return this.baseMapper.getDetailList(priceMasterId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SalePriceDetailDTO saveOrderDetail(SaleDetailBO saleDetailBO) throws Exception {
        SalePriceDetail priceDetatil = getPriceDetatil(saleDetailBO.getId());
        if (saleDetailBO.isAddDetail()) {
            priceDetatil.setGmtCreateId(saleDetailBO.getOperateUserId());
            priceDetatil.setGmtCreate(saleDetailBO.getOperateDate());
            priceDetatil.setDeleted(Boolean.parseBoolean(String.valueOf(DeleteEnum._0.getKey())));
        }
        BeanUtils.copyProperties(saleDetailBO, priceDetatil);
        priceDetatil.setGmtModifiedId(saleDetailBO.getOperateUserId());
        priceDetatil.setGmtModified(saleDetailBO.getOperateDate());
        this.saveOrUpdate(priceDetatil);
        return this.baseMapper.getPriceDetail(priceDetatil.getId());
    }

    @Override
    public List<SalePriceDetailDTO> getPriceDetailList(QuerySalePriceDetailBO queryBO) {
        QueryWrapper<SalePriceDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", queryBO.getSaleOrderId());
        queryWrapper.eq("deleted", false);

        return CopyUtils.copyList(this.baseMapper.selectList(queryWrapper), SalePriceDetailDTO.class);
    }

    private SalePriceDetail getPriceDetatil(Long id) {
        if (ObjectUtil.isEmpty(id)) {
            return new SalePriceDetail();
        }
        SalePriceDetail salePriceDetail = this.getById(id);
        return ObjectUtil.isNull(salePriceDetail) ? new SalePriceDetail() : salePriceDetail;
    }


    private static void initSalePriceDetatil(SalePriceDetail salePriceDetail, Long operateUserId, Date operateDate,
            String companyId) {
        salePriceDetail.setDeleted(Boolean.FALSE);
        salePriceDetail.setGmtCreate(operateDate);
        salePriceDetail.setGmtCreateId(operateUserId);
        salePriceDetail.setGmtModified(operateDate);
        salePriceDetail.setGmtModifiedId(operateUserId);
        salePriceDetail.setCompanyId(companyId);
    }

}
